package com.ruoyi.system.service.wx.pay.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.constant.BusinessConst;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.wx.WxUser;
import com.ruoyi.system.domain.wx.pay.WxPayShareMapper;
import com.ruoyi.system.mapper.wx.pay.WxPayShareMapperMapper;
import com.ruoyi.system.service.wx.impl.WxUserServiceImpl;
import com.ruoyi.system.service.wx.pay.IWxPayShareMapperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 支出分享映射Service业务层处理
 *
 * @author mic
 * @date 2023-08-18
 */
@Service
public class WxPayShareMapperServiceImpl implements IWxPayShareMapperService {
    @Autowired
    private WxPayShareMapperMapper wxPayShareMapperMapper;
    @Autowired
    private WxUserServiceImpl userService;

    /**
     * 查询支出分享映射
     *
     * @param belongId 支出分享映射主键
     * @return 支出分享映射
     */
    @Override
    public WxPayShareMapper selectWxPayShareMapperByBelongId(String belongId) {
        return wxPayShareMapperMapper.selectWxPayShareMapperByBelongId(belongId);
    }

    /**
     * 查询支出分享映射列表
     *
     * @param wxPayShareMapper 支出分享映射
     * @return 支出分享映射
     */
    @Override
    public List<WxPayShareMapper> selectWxPayShareMapperList(WxPayShareMapper wxPayShareMapper) {
        return wxPayShareMapperMapper.selectWxPayShareMapperList(wxPayShareMapper);
    }

    /**
     * 新增支出分享映射
     *
     * @param wxPayShareMapper 支出分享映射
     * @return 结果
     */
    @Override
    public int insertWxPayShareMapper(WxPayShareMapper wxPayShareMapper) {
        wxPayShareMapper.setCreateTime(DateUtils.getNowDate());
        return wxPayShareMapperMapper.insertWxPayShareMapper(wxPayShareMapper);
    }

    /**
     * 修改支出分享映射
     *
     * @param wxPayShareMapper 支出分享映射
     * @return 结果
     */
    @Override
    public int updateWxPayShareMapper(WxPayShareMapper wxPayShareMapper) {
        return wxPayShareMapperMapper.updateWxPayShareMapper(wxPayShareMapper);
    }

    /**
     * 批量删除支出分享映射
     *
     * @param belongIds 需要删除的支出分享映射主键
     * @return 结果
     */
    @Override
    public int deleteWxPayShareMapperByBelongIds(String[] belongIds) {
        return wxPayShareMapperMapper.deleteWxPayShareMapperByBelongIds(belongIds);
    }

    /**
     * 删除支出分享映射信息
     *
     * @param belongId 支出分享映射主键
     * @return 结果
     */
    @Override
    public int deleteWxPayShareMapperByBelongId(String belongId) {
        return wxPayShareMapperMapper.deleteWxPayShareMapperByBelongId(belongId);
    }

    /**
     * 返回被分享者id和备注名称
     *
     * @param list
     * @param flag 0:我创建 1:ta创建
     */
    @Override
    public List<JSONObject> handleViewBy(List<WxPayShareMapper> list, String flag) {
        List<JSONObject> jsonlist = new ArrayList<>();
        if (BusinessConst.YesOrNo.NO.equals(flag)) {
            // 我创建的
            for (int i = 0; i < list.size(); i++) {
                JSONObject json = new JSONObject();
                json.put("userId", list.get(i).getViewBy());
                json.put("name", StringUtils.isNotBlank(list.get(i).getViewName())
                        ? list.get(i).getViewName()
                        : this.getUserName(list.get(i).getViewBy()));
                jsonlist.add(json);
            }

        } else if (BusinessConst.YesOrNo.YES.equals(flag)) {
            // ta创建的
            for (int i = 0; i < list.size(); i++) {
                JSONObject json = new JSONObject();
                json.put("userId", list.get(i).getBelongBy());
                json.put("name", StringUtils.isNotBlank(list.get(i).getBelongName())
                        ? list.get(i).getBelongName()
                        : this.getUserName(list.get(i).getBelongBy()));
                jsonlist.add(json);
            }
        }
        return jsonlist;
    }

    /**
     * 备注名称为空时, 使用用户的显示id
     */
    public String getUserName(String uesrId) {
        WxUser wxUser = userService.selectWxUserByUserId(uesrId);
        if (wxUser != null) {
            return wxUser.getViewId() + "";
        } else {
            return "该用户已过期!";
        }
    }
}
